package com.maduar.mybatis.demo.mq;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeoutException;

/**
 * Description:
 *
 * @author maduar maduar@163.com
 * @date 2021-04-30 21:48
 */
public class RabbitmqDeadQProducer {
    private static final String EXCHANGE_NAME = "exchange_demo";
    private static final String ROUTING_KEY = "routingkey_demo";
    private static final String QUEUE_NAME = "queue_demo";
    private static final String IP_ADDRESS = "119.45.57.46";
    private static final int PORT = 5672;//RabbitMQ服务端默认端口号为5672

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(IP_ADDRESS);
        connectionFactory.setPort(PORT);
        connectionFactory.setUsername("root");
        connectionFactory.setPassword("root");

        Channel channel = null;
        try (Connection connection = connectionFactory.newConnection()) {

            channel = connection.createChannel();
            String exchange = "test_dlx_change";
            String routingKey = "dlx.save";
            String msg = "Hello Rabbit DLX Message";

            channel.exchangeDeclare(exchange, "direct");

            AMQP.BasicProperties properties = new AMQP.BasicProperties().builder()
                    .deliveryMode(2)
                    .contentEncoding("UTF-8")
                    .expiration("10000")
                    .build();

            channel.basicPublish(exchange, routingKey, true, properties, msg.getBytes());
        } catch (Exception e) {
            if (Objects.nonNull(channel)) {
                channel.close();
            }
        }

        System.out.println("MESSAGE SEND OK");
    }
}
